﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Other;
using PureMVC.Interfaces;
using System;

namespace PureMVC.Core
{
    public partial class Controller
    {
        protected const string MULTITON_MSG = "Controller instance for this Multiton key already constructed!";

        protected static Dictionary<string, WeakLazy<IController>> instanceMap =
            new Dictionary<string, WeakLazy<IController>>();

        public static IController GetInstance(string _key, Func<IController> _controllerFactory)
        {
            if (!instanceMap.ContainsKey(_key))
            {
                _controllerFactory();
                //instanceMap.Add(_key, new WeakLazy<IController>(_controllerFactory));
            }
            return instanceMap[_key].Value;
        }

        public static void RemoveController(string _key)
        {
            if (instanceMap.ContainsKey(_key))
            {
                instanceMap.Remove(_key);
            }
        }
    }
}